<?php

namespace app\Modules\Api\models;

use Yii;
use app\models\Push as BaseModel;

/**
 * Class Push
 * @package app\Modules\Api\models
 */
class Push extends BaseModel
{
    public function fields()
    {
        $fields = ['title','content'];
        $strvalfields = [
            'state',
            'pushid',
            'type',
        ];

        foreach($strvalfields as $strvalfield){
            $fields[$strvalfield] = function() use ($strvalfield){
                return strval($this->$strvalfield);
            };
        }

        $fields['created_at'] = function(){
            $time = $this->created_at;
            //获取今天凌晨的时间戳
            $day = strtotime(date('Y-m-d',time()));
            //获取昨天凌晨的时间戳
            $pday = strtotime(date('Y-m-d',strtotime('-1 day')));
            $str = '';
            if($time<=$pday){
                $str = date('Y-m-d',$time);
            }elseif($time<$day && $time>$pday){
                $str = "昨天";
            }else if($time>=$day){
                $str = date('G:i:s',$time);
            }
            return $str;
        };
        return $fields;
    }

    public static function getCount($user_id)
    {
        $count = self::find()
            ->where(['user_id'=>$user_id,'state'=>self::STATE_NOT_READ])
            ->count();
        return strval($count);
    }
}